/*
 * This file Copyright (c) 2015. Walle.
 * (http://www.wallellen.com). All rights reserved.
 *
 *
 * This file is dual-licensed under both the
 * Walle Agreement (WA) and the GNU General Public License.
 * You may elect to use one or the other of these licenses.
 *
 * This file is distributed in the hope that it will be
 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
 * Redistribution, except as permitted by whichever of the GPL
 * or WA you select, is prohibited.
 *
 * 1. For the GPL license (GPL), you can redistribute and/or
 * modify this file under the terms of the GNU General
 * Public License, Version 3, as published by the Free Software
 * Foundation.  You should have received a copy of the GNU
 * General Public License, Version 3 along with this program;
 * if not, write to the Free Software Foundation, Inc., 51
 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * 2. For the Walle Agreement (WA), this file
 * and the accompanying materials are made available under the
 * terms of the WA which accompanies this distribution, and
 * is available at http://www.wallellen.com/agreement.html
 *
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 */

package com.wallellen.authority;

import com.wallellen.authority.domain.Action;
import com.wallellen.authority.domain.User;
import com.wallellen.authority.service.ActionServiceInterface;
import com.wallellen.authority.service.UserServiceInterface;
import com.wallellen.core.AuthorityService;
import com.wallellen.core.auth.domain.AuthKey;
import com.wallellen.core.auth.domain.LoginUser;
import com.wallellen.core.auth.domain.WebMenu;
import com.wallellen.core.auth.exception.LoginUserNoExistException;
import com.wallellen.core.auth.util.AuthenticateInterface;
import com.wallellen.core.auth.util.AuthenticateProvider;
import com.wallellen.util.spring.BeanFactory;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by walle on 12/11/15.
 */
public class AuthorityServiceImpl implements AuthorityService {
    private static AuthorityServiceImpl service;
    private UserServiceInterface userServiceImpl;
    private ActionServiceInterface actionServiceImpl;
    private AuthenticateInterface authUtil = AuthenticateProvider.getInstance();

    public static AuthorityService getInstance() {
        if (service == null) {
            service = new AuthorityServiceImpl();
            service.userServiceImpl = BeanFactory.getBean("userServiceImpl", UserServiceInterface.class);
            service.actionServiceImpl = BeanFactory.getBean("actionServiceImpl", ActionServiceInterface.class);
        }
        return service;
    }

    public void loginHandle(String userId) {
        this.userServiceImpl.setLoginTime(userId);
    }

    public Set<String> getUserAuthKeys(String userId) {
        User user = this.userServiceImpl.getUserEntity(userId);
        List<Action> actions = null;
        if (user.getType().equals("3"))
            actions = this.actionServiceImpl.getAllActions();
        else {
            actions = this.userServiceImpl.getUserActions(userId);
        }
        Set<String> set = new HashSet();
        for (Action action : actions) {
            set.add(action.getAuthkey());
        }
        return set;
    }

    public LoginUser getUserById(String userId) {
        return this.userServiceImpl.getUserEntity(userId);
    }

    public LoginUser getUserByLoginName(String loginName) {
        return this.userServiceImpl.getUserByLoginName(loginName);
    }

    public List<WebMenu> getUserMenu(String userId) {
        User user = this.userServiceImpl.getUserEntity(userId);
        List list = null;
        if (user.getType().equals("3"))
            list = this.actionServiceImpl.getAllMenus();
        else {
            list = this.userServiceImpl.getUserMenus(userId);
        }
        return list;
    }

    public boolean isLegality(String loginName, String password) throws LoginUserNoExistException {
        User user = this.userServiceImpl.getUserByLoginName(loginName);
        if (user == null) {
            throw new LoginUserNoExistException("该登录名不存在！");
        }
        if (user.getType().equals("2")) {
            throw new LoginUserNoExistException("该用户无登录权限！");
        }
        if (!user.getState().equals("1")) {
            throw new LoginUserNoExistException("该用户已停用！");
        }
        if (this.authUtil.isMd5code(password)) {
            if (password.toUpperCase().equals(user.getPassword())) {
                return true;
            }
        } else if (this.authUtil.encodeLoginPasswordOnMd5(password, loginName).equals(user.getPassword())) {
            return true;
        }

        return false;
    }

    public AuthKey getAuthKey(String key) {
        return this.actionServiceImpl.getCacheAction(key);
    }

    public List<String> getUserPostKeys(String userId) {
        return this.userServiceImpl.getUserPostIds(userId);
    }

    public String getUserOrgKey(String userId) {
        return this.userServiceImpl.getUserOrganization(userId).getId();
    }

    public UserServiceInterface getUserServiceImpl() {
        return this.userServiceImpl;
    }

    public void setUserServiceImpl(UserServiceInterface userServiceImpl) {
        this.userServiceImpl = userServiceImpl;
    }

    public ActionServiceInterface getActionServiceImpl() {
        return this.actionServiceImpl;
    }

    public void setActionServiceImpl(ActionServiceInterface actionServiceImpl) {
        this.actionServiceImpl = actionServiceImpl;
    }
}
